package com.example.service.impl;

import com.example.constant.MessageConstant;
import com.example.exception.AccountNotFoundException;
import com.example.exception.PasswordErrorException;
import com.example.mapper.AdminMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.dto.UserLoginDto;
import com.example.pojo.entity.Admin;
import com.example.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.example.constant.StatusConstant.DISABLE;

@Service
@Slf4j
public class AdminService {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminMapper adminMapper;

    public Admin login(UserLoginDto userLoginDto){

        String phone = userLoginDto.getUserMobile();
        String password = userLoginDto.getUserPassword();
        //返回用户对象
        Admin admin = adminMapper.getAdminInfo(phone);
        //1.判断用户是否为空，如果为空标识登录失败，抛出业务异常
        if (admin == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //1.1不为空，判断密码是否匹配
        Boolean isPasswordMatch = (admin.getPassword()).equals(password);
        //1.2不匹配：返回密码错误异常
        if (!isPasswordMatch) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
//        1.3匹配：返回用户信息
        log.info("打印user" + admin);
        return admin;

    }
}
